package cn.newhopedairy.common.wrapper;

import cn.newhopedairy.common.enums.ApiStatusCode;
import lombok.Getter;

import java.io.Serializable;

/**
 * 请求结果包装类
 *
 * @author Neo.Li
 * @date 2020-09-09 17:59
 */
@Getter
public class R<T> implements Serializable {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 9527L;

    /**
     * 错误码
     */
    private int code;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 结果数据
     */
    private T data;

    /**
     * 用于反序列化框架调用
     */
    public R() {

    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok(T data) {
        return new R<>(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> fail(T data) {
        return new R<>(ApiStatusCode.FAILED.getCode(), ApiStatusCode.FAILED.getMsg(), data);
    }

    public static <T> R<T> create(StatusCode statusCode, T data) {
        return new R<>(statusCode.getCode(), statusCode.getMsg(), data);
    }

    public static <T> R<T> create(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}
